package com.cenntro.auth.utils;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;

import com.cenntro.auth.model.dbo.SysSooidDO;
import com.cenntro.common.exception.BizException;

import lombok.extern.slf4j.Slf4j;

/**
 * 
 * @author Administrator
 *
 */
@Slf4j
public class AgentPathUtil {
    
//    public static String getAgentPath(String agentPath){
//        Subject subject = SecurityUtils.getSubject();
//        SysSooidDO user = (SysSooidDO)subject.getPrincipal();
//        int roleType = (Integer)subject.getSession().getAttribute("roleType");
//        if (roleType == 1){    //admin
//            return StringUtils.isNotBlank(agentPath) ? agentPath : user.getAgentPath();
//        }else 
//        if(roleType == 2){//agent
//            return StringUtils.isNotBlank(agentPath) ? agentPath : user.getAgentPath();
//        }
//        return user.getAgentPath();
//    }
    
//    public static String getAgentPath4query(String agentPath){
//        Subject subject = SecurityUtils.getSubject();
//        SysSooidDO user = (SysSooidDO)subject.getPrincipal();
//        int roleType = (Integer)subject.getSession().getAttribute("roleType");
//        if(roleType == 2){//agent
//            return StringUtils.isNotBlank(agentPath) ? agentPath : user.getAgentPath();
//        }
//        return user.getAgentPath();
//    }
    
    /**
     * 
    * @Title 根据角色返回agentPath
    * @Description 管理员返回 null ，其他返回所属客户或所在子机构的agentPath
    * @param  agentPath 选择的agentPath
    * @return agentPath
     */
    public static String getAgentPathByRole(String agentPath){
        try{
            Subject subject = SecurityUtils.getSubject();
            SysSooidDO user = (SysSooidDO)subject.getPrincipal();
//            if(user == null){
//                return agentPath;
//            }
            int roleType = user.getRoleType();
            if (roleType == 1){    //admin
                return agentPath;
            }else if(roleType == 2){//agent
                return StringUtils.isNotBlank(agentPath) ? agentPath : user.getAgentPath();
            }
            return user.getAgentPath();
        }catch(Exception e){
            log.info("用户未登录，请先登录", e);
            throw new BizException(-9, "用户未登录，请先登录");
        }
    }
    /**
     * 
    * @Title getRoleType
    * @Description TODO
    * @param 
    * @return
     */
    public static Integer getRoleType(){
        Subject subject = SecurityUtils.getSubject();
        SysSooidDO user = (SysSooidDO)subject.getPrincipal();
        return user.getRoleType();
    }
}
